package com.lsk.servicemap.remote;

import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.constant.AMapConfigConstants;
import com.lsk.internalcommon.response.TraceResponse;
import com.mysql.cj.util.StringUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class TraceClient {
    @Value("${map.key}")
    private String mapKey;

    @Value("${map.sid}")
    private String sid;

    @Autowired
    private RestTemplate restTemplate;

    public ResponseResult add (String tid){
        //拼接url
        StringBuilder url = new StringBuilder();
        url.append(AMapConfigConstants.TRACE_ADD_URL);
        url.append("?");
        url.append("key="+mapKey);
        url.append("&");
        url.append("sid="+sid);
        url.append("&");
        url.append("tid="+tid);
        ResponseEntity<String> trace = restTemplate.postForEntity(url.toString(), null, String.class);
        String body = trace.getBody();
        JSONObject result = JSONObject.fromObject(body);
        JSONObject data = result.getJSONObject("data");
        String trid = data.getString("trid");

        String trname = "";
        TraceResponse traceResponse = new TraceResponse();
        if (data.has("trname")){
            trname = data.getString("trname");
        }
        traceResponse.setTrid(trid);
        traceResponse.setTrname(trname);
        return ResponseResult.success(traceResponse);
    }

}
